{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/cohere-ai/notebooks/blob/main/notebooks/guide/Fueling_Generative_Content_with_Keyword_Research.ipynb\">\n",
        "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
        "</a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5HkhthoeyQnV"
      },
      "source": [
        "# Fueling Generative Content with Keyword Research\n",
        "\n",
        "Generative models have proven extremely useful in content idea generation. But they don’t take into account user search demand and trends. In this notebook, let’s see how we can solve that by adding keyword research into the equation.\n",
        "\n",
        "Read the accompanying [blog post here](https://txt.cohere.ai/generative-content-keyword-research/)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Install packages\n",
        "! pip install cohere -q"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "4awUCb_3jw-v",
        "outputId": "ab8e8e13-99a8-47fc-8736-49a698476075"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "import cohere\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.cluster import KMeans\n",
        "\n",
        "import cohere\n",
        "co = cohere.Client(\"COHERE_API_KEY\") # Get your API key: https://dashboard.cohere.com/api-keys"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "#@title Enable text wrapping in Google Colab\n",
        "\n",
        "from IPython.display import HTML, display\n",
        "\n",
        "def set_css():\n",
        "  display(HTML('''\n",
        "  <style>\n",
        "    pre {\n",
        "        white-space: pre-wrap;\n",
        "    }\n",
        "  </style>\n",
        "  '''))\n",
        "get_ipython().events.register('pre_run_cell', set_css)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9YsKrQFsyZIE"
      },
      "source": [
        "# Step 1: Get a list of High-performing Keywords "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, we need to get a supply of high-traffic keywords for a given topic. We can get this via keyword research tools, of which are many available. We’ll use Google Keyword Planner, which is free to use."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "'remote_teams.csv'"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Download the pre-created dataset (feel free to replace with your CSV file, containing two columns - \"keyword\" and \"volume\")\n",
        "\n",
        "import wget\n",
        "wget.download(\"https://raw.githubusercontent.com/cohere-ai/notebooks/main/notebooks/data/remote_teams.csv\", \"remote_teams.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "_JdLS_FVyjfr",
        "outputId": "211c8b07-da89-4269-a652-2c164d09338d"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>keyword</th>\n",
              "      <th>volume</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>managing remote teams</td>\n",
              "      <td>1000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>remote teams</td>\n",
              "      <td>390</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>collaboration tools for remote teams</td>\n",
              "      <td>320</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>online games for remote teams</td>\n",
              "      <td>320</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>how to manage remote teams</td>\n",
              "      <td>260</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                keyword  volume\n",
              "0                 managing remote teams    1000\n",
              "1                          remote teams     390\n",
              "2  collaboration tools for remote teams     320\n",
              "3         online games for remote teams     320\n",
              "4            how to manage remote teams     260"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Create a dataframe\n",
        "df = pd.read_csv('remote_teams.csv')\n",
        "df.columns = [\"keyword\",\"volume\"]\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "whqG2M2Dylxu"
      },
      "source": [
        "# Step 2: Group the Keywords into Topics "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We now have a list of keywords, but this list is still raw. For example, “managing remote teams” is the top-ranking keyword in this list. But at the same time, there are many similar keywords further down in the list, such as “how to effectively manage remote teams.”\n",
        "\n",
        "We can do that by clustering them into topics. For this, we’ll leverage Cohere’s Embed endpoint and scikit-learn."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qJbbahQA0BJb"
      },
      "source": [
        "### Embed the Keywords with Cohere Embed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The Cohere Embed endpoint turns a text input into a text embedding."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "O7vz9gkXjGMI",
        "outputId": "d7f53d3d-e426-4433-a5be-1bb9c7d2b566"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "def embed_text(texts):\n",
        "  output = co.embed(\n",
        "                texts=texts,\n",
        "                model='embed-english-v3.0',\n",
        "                input_type=\"search_document\",\n",
        "                )\n",
        "  return output.embeddings\n",
        "\n",
        "embeds = np.array(embed_text(df['keyword'].tolist()))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GKnEI1E40Djn"
      },
      "source": [
        "### Cluster the Keywords into Topics with scikit-learn"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We then use these embeddings to cluster the keywords. A common term used for this exercise is “topic modeling.” Here, we can leverage scikit-learn’s KMeans module, a machine learning algorithm for clustering."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "jUAZueb0luKN",
        "outputId": "b05799e5-665d-4dcf-fb30-7eb7df1ed03f"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>keyword</th>\n",
              "      <th>volume</th>\n",
              "      <th>topic</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>managing remote teams</td>\n",
              "      <td>1000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>remote teams</td>\n",
              "      <td>390</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>collaboration tools for remote teams</td>\n",
              "      <td>320</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>online games for remote teams</td>\n",
              "      <td>320</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>how to manage remote teams</td>\n",
              "      <td>260</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                keyword  volume  topic\n",
              "0                 managing remote teams    1000      0\n",
              "1                          remote teams     390      1\n",
              "2  collaboration tools for remote teams     320      1\n",
              "3         online games for remote teams     320      3\n",
              "4            how to manage remote teams     260      0"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "NUM_TOPICS = 4\n",
        "kmeans = KMeans(n_clusters=NUM_TOPICS, random_state=21, n_init=\"auto\").fit(embeds)\n",
        "df['topic'] = list(kmeans.labels_)\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fLUzQoJD0IdY"
      },
      "source": [
        "### Generate Topic Names with Cohere Chat"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We use the Chat to generate a topic name for that cluster."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Group the DataFrame by 'topic' and aggregate the 'keyword' column into sets (which automatically removes duplicates)\n",
        "topic_keywords_dict = {topic: list(set(group['keyword'])) for topic, group in df.groupby('topic')}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Function to generate a topic name based on keywords\n",
        "def generate_topic_name(keywords):\n",
        "    # Construct the prompt\n",
        "    prompt = f\"\"\"Generate a concise topic name that best represents these keywords.\\\n",
        "Provide just the topic name and not any additional details.\n",
        "\n",
        "Keywords: {', '.join(keywords)}\"\"\"\n",
        "    \n",
        "    # Call the Cohere API\n",
        "    response = co.chat(\n",
        "        model='command-r',  # Choose the model size\n",
        "        message=prompt,\n",
        "        preamble=\"\")\n",
        "    \n",
        "    # Return the generated text\n",
        "    return response.text"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>keyword</th>\n",
              "      <th>volume</th>\n",
              "      <th>topic</th>\n",
              "      <th>topic_name</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>managing remote teams</td>\n",
              "      <td>1000</td>\n",
              "      <td>0</td>\n",
              "      <td>Remote Team Management</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>remote teams</td>\n",
              "      <td>390</td>\n",
              "      <td>1</td>\n",
              "      <td>Remote Team Tools and Tips</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>collaboration tools for remote teams</td>\n",
              "      <td>320</td>\n",
              "      <td>1</td>\n",
              "      <td>Remote Team Tools and Tips</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>online games for remote teams</td>\n",
              "      <td>320</td>\n",
              "      <td>3</td>\n",
              "      <td>Remote Team Fun</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>how to manage remote teams</td>\n",
              "      <td>260</td>\n",
              "      <td>0</td>\n",
              "      <td>Remote Team Management</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                                keyword  volume  topic  \\\n",
              "0                 managing remote teams    1000      0   \n",
              "1                          remote teams     390      1   \n",
              "2  collaboration tools for remote teams     320      1   \n",
              "3         online games for remote teams     320      3   \n",
              "4            how to manage remote teams     260      0   \n",
              "\n",
              "                   topic_name  \n",
              "0      Remote Team Management  \n",
              "1  Remote Team Tools and Tips  \n",
              "2  Remote Team Tools and Tips  \n",
              "3             Remote Team Fun  \n",
              "4      Remote Team Management  "
            ]
          },
          "execution_count": 23,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Generate topic names and create a mapping of topic number to topic name\n",
        "topic_name_mapping = {topic: generate_topic_name(keywords) for topic, keywords in topic_keywords_dict.items()}\n",
        "\n",
        "# Use the mapping to create a new column in the DataFrame\n",
        "df['topic_name'] = df['topic'].map(topic_name_mapping)\n",
        "\n",
        "# Display the first few rows to verify the new column\n",
        "df.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Topic 0: Remote Team Management\n",
            "Topic 1: Remote Team Tools and Tips\n",
            "Topic 2: Remote Team Resources\n",
            "Topic 3: Remote Team Fun\n"
          ]
        }
      ],
      "source": [
        "# View the list of topics\n",
        "for topic, name in topic_name_mapping.items():\n",
        "    print(f\"Topic {topic}: {name}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pMQzL5sL0YNN"
      },
      "source": [
        "# Step 3: Generate Blog Post Ideas for Each Topic"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now that we have the keywords nicely grouped into topics, we can proceed to generate the content ideas.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1tlIS2db0fWU"
      },
      "source": [
        "### Take the Top Keywords from Each Topic"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here we can implement a filter to take just the top N keywords from each topic, sorted by the search volume. In our case, we use 10."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "1bnYoTUCGQTv",
        "outputId": "cabdd711-641f-44e7-b7c5-19bf85f2512c"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "TOP_N = 10\n",
        "\n",
        "# Group the DataFrame by topic and select the top N keywords sorted by volume\n",
        "top_keywords = (df.groupby('topic')\n",
        "                        .apply(lambda x: x.nlargest(TOP_N, 'volume'))\n",
        "                        .reset_index(drop=True))\n",
        "\n",
        "\n",
        "# Convert the DataFrame to a nested dictionary\n",
        "content_by_topic = {}\n",
        "for topic, group in top_keywords.groupby('topic'):\n",
        "    keywords = ', '.join(list(group['keyword']))\n",
        "    topic2name = topic2name = dict(df.groupby('topic')['topic_name'].first())\n",
        "    topic_name = topic2name[topic]\n",
        "    content_by_topic[topic] = {'topic_name': topic_name, 'keywords': keywords}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 285
        },
        "id": "erNJQT3a5kkD",
        "outputId": "f8b0c153-0bc9-46bd-b7fe-e5d0283dc3ab"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "{0: {'topic_name': 'Remote Team Management',\n",
              "  'keywords': 'managing remote teams, how to manage remote teams, leading remote teams, managing remote teams best practices, remote teams best practices, best practices for managing remote teams, manage remote teams, building culture in remote teams, culture building for remote teams, managing remote teams training'},\n",
              " 1: {'topic_name': 'Remote Team Tools and Tips',\n",
              "  'keywords': 'remote teams, collaboration tools for remote teams, team building for remote teams, scrum remote teams, tools for remote teams, zapier remote teams, working agreements for remote teams, working with remote teams, free collaboration tools for remote teams, free retrospective tools for remote teams'},\n",
              " 2: {'topic_name': 'Remote Team Resources',\n",
              "  'keywords': 'best collaboration tools for remote teams, slack best practices for remote teams, best communication tools for remote teams, best tools for remote teams, always on video for remote teams, best apps for remote teams, best free collaboration tools for remote teams, best games for remote teams, best gifts for remote teams, best ice breaker questions for remote teams'},\n",
              " 3: {'topic_name': 'Remote Team Fun',\n",
              "  'keywords': 'online games for remote teams, team building activities for remote teams, games for remote teams, retrospective ideas for remote teams, team building ideas for remote teams, fun retrospective ideas for remote teams, retro ideas for remote teams, team building exercises for remote teams, trust building exercises for remote teams, activities for remote teams'}}"
            ]
          },
          "execution_count": 27,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Print the topics and they top keywords\n",
        "content_by_topic"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VCXY5cNj0qAW"
      },
      "source": [
        "### Create a Prompt with These Keywords"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we use the Chat endpoint to produce the content ideas. The prompt we’ll use is as follows"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 17
        },
        "id": "M-g_FiA6fX2w",
        "outputId": "d4545e7c-ca43-4b58-ac87-2f2c4227eadc"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "def generate_blog_ideas(keywords):\n",
        "  prompt = f\"\"\"{keywords}\\n\\nThe above is a list of high-traffic keywords obtained from a keyword research tool. \n",
        "Suggest three blog post ideas that are highly relevant to these keywords. \n",
        "For each idea, write a one paragraph abstract about the topic. \n",
        "Use this format:\n",
        "Blog title: <text>\n",
        "Abstract: <text>\"\"\"\n",
        "  \n",
        "  response = co.chat(\n",
        "    model='command-r',\n",
        "    message = prompt)\n",
        "  return response.text\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wTV6GG8i0tdK"
      },
      "source": [
        "### Generate Content Ideas"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next, we generate the blog post ideas. It takes in a string of keywords, calls the Chat endpoint, and returns the generated text."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "-qGuVkIfmJ_d",
        "outputId": "5b7588cb-cfda-4590-80ca-532fceb6f125"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\n",
              "  <style>\n",
              "    pre {\n",
              "        white-space: pre-wrap;\n",
              "    }\n",
              "  </style>\n",
              "  "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Topic Name: Remote Team Management\n",
            "\n",
            "Top Keywords: managing remote teams, how to manage remote teams, leading remote teams, managing remote teams best practices, remote teams best practices, best practices for managing remote teams, manage remote teams, building culture in remote teams, culture building for remote teams, managing remote teams training\n",
            "\n",
            "Blog Post Ideas: Here are three blog post ideas:\n",
            "\n",
            "1. Blog title: \"Leading Remote Teams: Strategies for Effective Management\"\n",
            "   Abstract: Effective management of remote teams is crucial for success, but it comes with unique challenges. This blog will explore practical strategies for leading dispersed employees, focusing on building a cohesive and productive virtual workforce. It will cover topics such as establishing clear communication protocols, fostering a collaborative environment, and the importance of trusting and empowering your remote employees for enhanced performance.\n",
            "\n",
            "2. Blog title: \"Remote Teams' Best Practices: Creating a Vibrant and Engaging Culture\"\n",
            "   Abstract: Building a rich culture in a remote team setting is essential for employee engagement and retention. The blog will delve into creative ways to foster a sense of community and connection among team members who may be scattered across the globe. It will offer practical tips on creating virtual rituals, fostering open communication, and harnessing the power of technology for cultural development, ensuring remote employees feel valued and engaged.\n",
            "\n",
            "3. Blog title: \"Managing Remote Teams: A Comprehensive Guide to Training and Development\"\n",
            "   Abstract: Training and developing remote teams present specific challenges and opportunities. This comprehensive guide will arm managers with techniques to enhance their remote team's skills and knowledge. It will explore the latest tools and methodologies for remote training, including virtual workshops, e-learning platforms, and performance coaching. Additionally, the blog will discuss the significance of ongoing development and how to create an environment that encourages self-improvement and learning.\n",
            "\n",
            "Each of these topics explores a specific aspect of managing remote teams, providing valuable insights and practical guidance for leaders and managers in the evolving remote work landscape.\n",
            "\n",
            "--------------------------------------------------\n",
            "Topic Name: Remote Team Tools and Tips\n",
            "\n",
            "Top Keywords: remote teams, collaboration tools for remote teams, team building for remote teams, scrum remote teams, tools for remote teams, zapier remote teams, working agreements for remote teams, working with remote teams, free collaboration tools for remote teams, free retrospective tools for remote teams\n",
            "\n",
            "Blog Post Ideas: 1. Blog title: \"The Ultimate Guide to Building Effective Remote Teams\"\n",
            "   Abstract: Building a cohesive and productive remote team can be challenging. This blog will serve as a comprehensive guide, offering practical tips and insights on how to create a united and successful virtual workforce. It will cover essential topics such as building a strong team culture, utilizing collaboration tools, and fostering effective communication strategies, ensuring remote teams can thrive and achieve their full potential.\n",
            "\n",
            "2. Blog title: \"The Best Collaboration Tools for Remote Teams: A Comprehensive Review\"\n",
            "   Abstract: With the rapid rise of remote work, collaboration tools have become essential for teams' productivity and efficiency. This blog aims to review and compare the most popular collaboration tools, providing an in-depth analysis of their features, ease of use, and benefits. It will offer insights into choosing the right tools for remote collaboration, helping teams streamline their workflows and enhance their overall performance.\n",
            "\n",
            "3. Blog title: \"Remote Retrospective: A Guide to Reflect and Grow as a Remote Team\"\n",
            "   Abstract: Conducting effective retrospectives is crucial for remote teams to reflect on their experiences, learn from the past, and chart a course for the future. This blog will focus on remote retrospectives, exploring different formats, techniques, and free tools that teams can use to foster continuous improvement. It will also provide tips on creating a safe and inclusive environment, encouraging honest feedback and productive discussions.\n",
            "\n",
            "--------------------------------------------------\n",
            "Topic Name: Remote Team Resources\n",
            "\n",
            "Top Keywords: best collaboration tools for remote teams, slack best practices for remote teams, best communication tools for remote teams, best tools for remote teams, always on video for remote teams, best apps for remote teams, best free collaboration tools for remote teams, best games for remote teams, best gifts for remote teams, best ice breaker questions for remote teams\n",
            "\n",
            "Blog Post Ideas: 1. Blog title: \"The Ultimate Guide to Remote Team Collaboration Tools\"\n",
            "   Abstract: With the rise of remote work, choosing the right collaboration tools can be crucial to a team's success and productivity. This blog aims to be an comprehensive guide, outlining the various types of tools available, from communication platforms like Slack to project management software and online collaboration tools. It will offer best practices and guidelines for selecting and utilizing these tools, ensuring remote teams can work seamlessly together and maximize their output.\n",
            "\n",
            "2. Blog title: \"Remote Team Management: Tips for Leading a Successful Virtual Workforce\"\n",
            "   Abstract: Managing a remote team comes with its own set of challenges. This blog will provide an in-depth exploration of best practices for leading and motivating virtual teams. Covering topics such as effective communication strategies, performance evaluation, and maintaining a cohesive team culture, it will offer practical tips for managers and leaders to ensure their remote teams are engaged, productive, and well-managed.\n",
            "\n",
            "3. Blog title: \"The Fun Side of Remote Work: Games, Icebreakers, and Team Building Activities\"\n",
            "   Abstract: Remote work can be isolating, and this blog aims to provide some fun and creative solutions. It will offer a comprehensive guide to the best online games, icebreaker questions, and virtual team building activities that remote teams can use to connect and bond. From virtual escape rooms to interactive games and thought-provoking icebreakers, these ideas will help enhance team spirit, foster collaboration, and create a enjoyable remote work experience.\n",
            "\n",
            "--------------------------------------------------\n",
            "Topic Name: Remote Team Fun\n",
            "\n",
            "Top Keywords: online games for remote teams, team building activities for remote teams, games for remote teams, retrospective ideas for remote teams, team building ideas for remote teams, fun retrospective ideas for remote teams, retro ideas for remote teams, team building exercises for remote teams, trust building exercises for remote teams, activities for remote teams\n",
            "\n",
            "Blog Post Ideas: 1. Blog title: \"The Great Remote Retro: Fun Games and Activities for Your Team\"\n",
            "   Abstract: Remote work can make team building challenging. This blog post will be a fun guide to hosting interactive retro games and activities that bring your remote team together. From online escape rooms to virtual scavenger hunts, we'll explore the best ways to engage and unite your team, fostering collaboration and camaraderie. Virtual icebreakers and retrospective ideas will also be included to make your remote meetings more interactive and enjoyable.\n",
            "\n",
            "2. Blog title: \"Trust Falls: Building Trust Among Remote Teams\"\n",
            "   Abstract: Trust is the foundation of every successful team, but how do you build it when everyone is scattered across different locations? This blog will focus on trust-building exercises and activities designed specifically for remote teams. From virtual trust falls to transparent communication practices, we'll discover innovative ways to strengthen team bonds and foster a culture of trust. You'll learn how to create an environment where your remote team can thrive and collaborate effectively.\n",
            "\n",
            "3. Blog title: \"Game Night for Remote Teams: A Guide to Online Games and Activities\"\n",
            "   Abstract: Miss the old office game nights? This blog will bring the fun back with a guide to hosting online game nights and activities that are perfect for remote teams. From trivia games to virtual board games and even remote-friendly outdoor adventures, we'll keep your team engaged and entertained. With tips on setting up online tournaments and ideas for encouraging participation, your virtual game nights will be the highlight of your team's week. Keep your remote team spirit high!\n",
            "\n",
            "--------------------------------------------------\n"
          ]
        }
      ],
      "source": [
        "# Generate content ideas\n",
        "for key,value in content_by_topic.items():\n",
        "  value['ideas'] = generate_blog_ideas(value['keywords'])\n",
        "\n",
        "\n",
        "# Print the results\n",
        "for key,value in content_by_topic.items():\n",
        "  print(f\"Topic Name: {value['topic_name']}\\n\")\n",
        "  print(f\"Top Keywords: {value['keywords']}\\n\")\n",
        "  print(f\"Blog Post Ideas: {value['ideas']}\\n\")\n",
        "  print(\"-\"*50)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Anr0Hu3hLxE3"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
